<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card v-loading="loading">
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="clickShowAdd"
            >新增角色</el-button>
            <el-dialog :title="showTitle" :visible="showDialog" :close-on-click-modal="false" @close="closeDialog">
              <el-form ref="roleForm" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入角色名称" />
                </el-form-item>
                <el-form-item label="角色描述">
                  <el-input v-model="form.description" placeholder="请输入角色描述" />
                </el-form-item>
              </el-form>

              <template #footer>
                <el-button @click="closeDialog">取消</el-button>
                <el-button type="primary" @click="roleAddButton">确认</el-button>
              </template>
            </el-dialog>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column label="序号" width="100" type="index" :index="indexMethod" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success" @click="clickAssignDialog(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="handleEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="handleDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- current-page 表示的当前是哪一页 -->
            <!-- page-sizes 动态设置页容量的下拉列表 -->
            <!-- page-size 表示的是当前的页容量 -->
            <!-- layout 表示的分页的展示样式 -->
            <!-- total 表示的 一共有多少条 -->
            <!-- @current-change 表示的点击页码的时候执行的事件-->
            <!-- @size-change 表示的动态设置页容量的时候执行的事件 -->
            <div style="margin-top:10px;display:flex;justify-content:flex-end">
              <el-pagination :current-page="page" :page-sizes="[2,3,4,5]" :page-size="pagesize" :total="total" layout="total, sizes, prev, pager, next, jumper" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
            </div>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 公司信息 -->

            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyForm.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyForm.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>

          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 分配权限弹层 -->
      <el-dialog title="分配权限" :visible="showAssignDialog" :close-on-click-modal="false" @close="closeAssignDialog" @open="clickOpen">
        <!-- show-checkbox 表示节点是否可以被选择 -->
        <!-- check-strictly 父子节点不关联 -->
        <!-- default-expand-all 是否默认展开 -->
        <el-tree ref="tree" v-loading="isTreeLoading" :data="permissionList" :props="{label: 'name'}" show-checkbox check-strictly default-expand-all node-key="id" />
        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeAssignDialog">取消</el-button>
            <el-button type="primary" @click="assignPerm">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleListApi, delRoleInfoApi, addRoleInfoApi, editRoleApi, getRoleDetailApi, getCompanyInfoApi, assignPerApi } from '@/api/setting'
import { mapGetters } from 'vuex'
import { transListToTreeData } from '@/utils/index'
import { getPermissionListApi } from '@/api/permission'
export default {
  name: 'Setting',
  data() {
    return {
      page: 1,
      pagesize: 2,
      roleList: [],
      total: 0,
      loading: false,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '填写角色名称', trigger: 'blur' }]
      },
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      roleId: '', // 分配权限角色id
      showAssignDialog: false,
      permissionList: [],
      isTreeLoading: false
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    showTitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 提交分配权限
    async assignPerm() {
      const permIds = this.$refs.tree.getCheckedKeys()
      await assignPerApi({ id: this.roleId, permIds })
      this.$message.success('分配权限成功')
      this.closeAssignDialog()
    },
    // 权限open事件
    async clickOpen() {
      this.isTreeLoading = true
      await this.getPromissionList()
      await this.getRoleDetailFn()
      this.isTreeLoading = false
    },
    // 回显权限 角色详细信息
    async getRoleDetailFn() {
      const { data: { permIds }} = await getRoleDetailApi(this.roleId)
      // 回显
      this.$refs.tree.setCheckedKeys(permIds)
    },
    // 获取角色数据
    async getPromissionList() {
      const { data } = await getPermissionListApi()
      // console.log(res)
      this.permissionList = transListToTreeData(data, '0')
    },
    // 点击分配权限
    clickAssignDialog(id) {
      this.roleId = id
      this.showAssignDialog = true
    },
    // 关闭分配权限弹层
    closeAssignDialog() {
      this.showAssignDialog = false
    },
    // 获取公司信息
    async getCompanyInfo() {
      const { data } = await getCompanyInfoApi(this.companyID)
      this.companyForm = data
    },
    // 编辑
    async handleEdit(id) {
      // 获取信息回显
      const { data } = await getRoleDetailApi(id)
      // console.log(data)
      this.form = data
      this.showDialog = true
    },
    // 添加
    roleAddButton() {
      this.$refs.roleForm.validate(async(valid) => {
        if (!valid) return
        if (this.form.id) {
          // 编辑
          await editRoleApi(this.form)
          this.$message.success('修改成功')
        } else {
          await addRoleInfoApi(this.form)
          this.$message.success('添加成功')
        }
        this.getRoleList()
        this.closeDialog()
      })
    },
    // 添加 关闭弹层
    closeDialog() {
      this.showDialog = false
      // 清空的是 绑定了prop的表单域的状态和数据
      this.$refs.roleForm.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    // 添加 显示弹层
    clickShowAdd() {
      this.showDialog = true
    },
    // 删除
    handleDel(id) {
      // console.log(a)
      // console.log(b)
      this.$confirm('是否确认删除', '删除').then(async() => {
        const res = await delRoleInfoApi(id)
        console.log(res)
        if (this.roleList.length === 1 && this.page > 1) {
          this.page--
        }
        this.$message.success('删除成功')
        this.getRoleList()
      }).catch(error => {
        console.log(error)
      })
    },
    // 页码更改
    indexMethod(index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    // 获取公司数据
    async getRoleList() {
      this.loading = true
      const { data } = await getRoleListApi(this.page, this.pagesize)
      console.log(data)
      this.roleList = data.rows
      this.total = data.total
      this.loading = false
    },
    /**
     * 点击页码
     */
    handleCurrentChange(val) {
      // console.log(val)
      this.page = val
      this.getRoleList()
    },
    /**
     * 修改页容量
     */
    handleSizeChange(val) {
      // console.log(val)
      this.pagesize = val
      this.getRoleList()
    }
  }
}
</script>

<style>

</style>
